package kordegi.forumsystem.config;

import kordegi.forumsystem.redis.RedisCacheManager;
import kordegi.forumsystem.redis.RedisSessionDAO;
import kordegi.forumsystem.shiro.JwtFilter;
import kordegi.forumsystem.shiro.UserRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
 * @author kordegi
 * @date 2022.05.04 0:04
 * @Description
 */
@Configuration
public class ShiroConfig {

    @Autowired
    JwtFilter jwtFilter;

    /**
     * @Description 设置session的存储器
     * @param redisSessionDAO
     * @return
     */
//    @Bean
//    public SessionManager sessionManager (RedisSessionDAO redisSessionDAO) {
//        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
//        sessionManager.setSessionDAO(redisSessionDAO);
//        return sessionManager;
//    }

    /**
     * @Description 设置defaultSecurityManager， 关闭session
     * @Mentioning 注意要是 DefaultWebSecurityManager！！！
     * @param userRealm
     * @return
     */
    @Bean
    public DefaultSecurityManager securityManager(/**SessionManager sessionManager, **/UserRealm userRealm/**, RedisCacheManager redisCacheManager**/) {
        //设置realm
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(userRealm);
        //和threadContext进行绑定
        ThreadContext.bind(securityManager);

//        securityManager.setSessionManager(sessionManager);
//        securityManager.setCacheManager(redisCacheManager);

        //关闭session
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);

        return securityManager;
    }

    /**
     * @Description 拦截器：添加含有jwt的map作为拦截路径
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/**", "jwt");
        chainDefinition.addPathDefinitions(filterMap);
        return chainDefinition;
    }

    /**
     * @Description 过滤器，把自定义的filter加进去
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean (SecurityManager securityManager, ShiroFilterChainDefinition shiroFilterChainDefinition) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //过滤器
        Map<String, Filter> filters = new HashMap<>();
        //jwt形式
        filters.put("jwt", jwtFilter);
        shiroFilterFactoryBean.setFilters(filters);
        //拦截器
        Map<String, String> filterMap = shiroFilterChainDefinition.getFilterChainMap();
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    /**
     * @Description 开启注释代理
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor (SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    /**
     * @Description shiro和 aop权限冲突问题： 将 usePrefix 或 proxyTargetClass 二者任一设置为 true 即可
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }

}
